<?php

class TintucsController extends BaseController {

    /**
     * Bai_viet Repository
     *
     * @var Bai_viet
     */
    protected $bai_viet;

    public function __construct(Bai_viet $bai_viet) {
        $this->bai_viet = $bai_viet;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $bai_viet_model = new Bai_viet();
        $bai_viets = $bai_viet_model->get_dsbaiviet_limit();

        return View::make('bai_viets.index', compact('bai_viets'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        $chuyen_muc = DB::table('chuyen_muc_bai_viets')->get();
        return View::make('bai_viets.create')->with('chuyen_muc', $chuyen_muc);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $input = Input::all();
        $bai_viet = new Bai_viet();
        $thu_tu = $bai_viet->get_max_thutu();

        $input['thu_tu_bai_viet'] =  $thu_tu+1;
        $input['TaiKhoan_Id'] = Auth::user()->id;
        $validation = Validator::make($input, Bai_viet::$rules);

        if ($validation->passes()) {
            $this->bai_viet->create($input);

            return Redirect::route('news.index');
        }

        return Redirect::route('news.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $bai_viet = $this->bai_viet->findOrFail($id);

        return View::make('bai_viets.show', compact('bai_viet'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $bai_viet = $this->bai_viet->find($id);
        $chuyen_muc_bai_viet = new Chuyen_muc_bai_viet();
        $results = $chuyen_muc_bai_viet->getAllchuyenmuc();
        if (is_null($bai_viet)) {
            return Redirect::route('news.index');
        }

        return View::make('bai_viets.edit', compact('bai_viet'))->with('chuyen_muc', $results);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $input = array_except(Input::all(), '_method');
        $input['TaiKhoan_Id'] = Auth::user()->id;
        $validation = Validator::make($input, Bai_viet::$rules);

        if ($validation->passes()) {
            $bai_viet = $this->bai_viet->find($id);
            $bai_viet->update($input);

            return Redirect::route('news.show', $id);
        }

        return Redirect::route('news.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->bai_viet->find($id)->delete();

        return Redirect::route('news.index');
    }

}
